import tesserocr
from PIL import Image
import requests
from io import BytesIO

from pytesseract import pytesseract

tessdaata_dir_config = r'--tessdata-dir /opt/homebrew/Cellar/tesseract/5.5.0/share/tessdata'
# 图片的URL
url = 'https://pic.616pic.com/ys_bnew_img/00/42/61/V3N7MK0v8e.jpg'

# 设置请求头，包括User-Agent
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
}

# 设置代理服务器
proxies = {
    'http': 'http://127.0.0.1:7890',
    'https': 'https://127.0.0.1:7890',
}

# 识别在线图片
response = requests.get(url)
response.raise_for_status()  # 如果请求失败, 抛出异常 HttpError
print(response.headers['Content-Type'])  # 输出图片类型

img2 = Image.open(BytesIO(response.content))

img2.save('yanzhengma.jpg')  # 保存以验证
tmp = pytesseract.image_to_string(Image.open('image/yanzhengma.jpg'), lang='eng', config=tessdaata_dir_config)
print(tmp)
